use rocket::{get,post};
use tools::date::calculate_interval;
use core::user::{LoginRequest,SimpleAuthService };
use serde::{Deserialize, Serialize};
use rocket::serde::json::Json;


// 需要传入的值
#[derive(Deserialize)]
struct LoginDto {
    username: String,
    password: String,
}

// 响应值
#[derive(Serialize)]
struct LoginResult {
    success: bool,
    token: Option<String>,
}

#[post("/login", data = "<dto>")]
pub fn user_login(dto: Json<LoginDto>) -> Json<LoginResult> {
    let req = LoginRequest {
        username: dto.username.clone(),
        password: dto.password.clone(),
    };

    let service = SimpleAuthService;
    if let Some(resp) = service.login(&req) {
        Json(LoginResult {
            success: true,
            token: Some(resp.token),
        })
    } else {
        Json(LoginResult {
            success: false,
            token: None,
        })
    }
}
#[post("/register")]
pub fn user_register() -> &'static str {
    "Hello, world!"
}